package com.zzh.takeoutapp.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zzh.takeoutapp.R;
import com.zzh.takeoutapp.base.MyBaseAdapter;
import com.zzh.takeoutapp.entity.HomeShopEntity;
import com.zzh.takeoutapp.utils.ImageManager;
import com.zzh.takeoutapp.view.DiscountsView;
import com.zzh.takeoutapp.view.FilterView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/7/25.
 */

public class ClassifyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private int TYPE_TOP0 = 0;
    private int TYPE_TOP1 = 1;
    private int TYPE_TOP2 = 2;
    private int ITEM_TYPE_BOTTOM = 3;
    private int TYPE_TOP6 = 4;
    private TypetyListpeHolder holder;
    private List<HomeShopEntity.DataBean.ItemsBean> entity;
    private int count = 3;
    //判断当前item是否是FooterView
    private int mBottomCount = 1;//底部View个数
    private Context mContext;

    public ClassifyAdapter(Context context) {
        this.mContext = context;
    }

    public void insert(List<HomeShopEntity.DataBean.ItemsBean> person, int position) {
        entity.addAll(position - count, person);
        notifyItemInserted(position - count);
    }

    public boolean isBottomView(int position) {
        return mBottomCount != 0 && position >= (count + entity.size());
    }

    public TypetyListpeHolder getHolder() {
        return holder;
    }

    public void setHolder(TypetyListpeHolder holder) {
        this.holder = holder;
    }


    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP0;
        } else if (position == 1) {
            return TYPE_TOP1;
        } else if (position == 2) {
            return TYPE_TOP2;
        } else if (mBottomCount != 0 && position >= (count + (entity != null ? entity.size() : 0))) {
            return ITEM_TYPE_BOTTOM;
        } else
            return TYPE_TOP6;
    }

    public List<HomeShopEntity.DataBean.ItemsBean> getEntity() {
        return entity;
    }

    public void setEntity(List<HomeShopEntity.DataBean.ItemsBean> entity) {
        this.entity = entity;
    }

    //底部 ViewHolder
    public class BottomViewHolder extends RecyclerView.ViewHolder {
        public BottomViewHolder(View itemView) {
            super(itemView);
        }
    }

    public class TextHolder extends BaseViewHolder {
        @Bind(R.id.ev_search)
        AppCompatEditText ev_search;

        public TextHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public TypetypeHolder typetypeHolder;

    public TypetypeHolder getTypetypeHolder() {
        return typetypeHolder;
    }

    public void setTypetypeHolder(TypetypeHolder typetypeHolder) {
        this.typetypeHolder = typetypeHolder;
    }

    public class TypetypeHolder extends BaseViewHolder {
        @Bind(R.id.filter_view)
        FilterView filterView;

        public FilterView getFilterView() {
            return filterView;
        }

        public void setFilterView(FilterView filterView) {
            this.filterView = filterView;
        }

        public TypetypeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public class TypeItemHolder extends BaseViewHolder {
        @Bind(R.id.food_main)
        LinearLayout food_main;
        @Bind(R.id.size_flow_layout)
        LinearLayout linearLayout;
        @Bind(R.id.rb)
        RatingBar rb;
        @Bind(R.id.tv_juli_label)
        TextView tv_juli_label;//距离
        @Bind(R.id.tv_orders)
        TextView tv_orders;//销售数量
        @Bind(R.id.tv_avg_score)
        TextView tv_avg_score;//评价
        @Bind(R.id.min_amount)
        TextView min_amount;//起送价
        @Bind(R.id.title)
        TextView title;//商家标题
        @Bind(R.id.draw_goods)
        ImageView draw_goods;//商家头像

        public TypeItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    @Override
    public int getItemCount() {
        int i = entity != null ? entity.size() : 0;
        return count + i + mBottomCount;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View views_item = LayoutInflater.from(mContext).inflate(R.layout.item_home_order, parent, false);
        View views_classify = LayoutInflater.from(mContext).inflate(R.layout.include_classify_layout, parent, false);
        View view_discounts = LayoutInflater.from(mContext).inflate(R.layout.include_text_discounts, parent, false);
        View views_text = LayoutInflater.from(mContext).inflate(R.layout.include_text_view, parent, false);
        if (viewType == TYPE_TOP0) {
            return new TextHolder(views_classify);
        } else if (viewType == TYPE_TOP1) {
            typetypeHolder = new TypetypeHolder(views_text);
            return typetypeHolder;
        } else if (viewType == TYPE_TOP2) {
            holder = new TypetyListpeHolder(view_discounts);
            return holder;
        } else if (viewType == TYPE_TOP6) {
            return new TypeItemHolder(views_item);
        } else {
            View views = LayoutInflater.from(mContext).inflate(R.layout.item_user_adapter, parent, false);
            int intw = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            int inth = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
            views_item.measure(intw, inth);
            view_discounts.measure(intw, inth);
            views_classify.measure(intw, inth);
            views_text.measure(intw, inth);
            WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
            int height = wm.getDefaultDisplay().getHeight();
            ViewGroup.LayoutParams lp = views.getLayoutParams();
            lp.height = height;
            if (entity != null && entity.size() < 5) {
                int view_height = height
                        - views_item.getMeasuredHeight() * (entity.size())
                        - view_discounts.getMeasuredHeight();
                ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(
                        ViewGroup.LayoutParams.MATCH_PARENT, view_height);
                views.setLayoutParams(layoutParams);
            }
            return new BottomViewHolder(views);
        }
    }

    public class TypetyListpeHolder extends BaseViewHolder {
        @Bind(R.id.discounts_view)
        DiscountsView real_filterView;

        public DiscountsView getReal_filterView() {
            return real_filterView;
        }

        public void setReal_filterView(DiscountsView real_filterView) {
            this.real_filterView = real_filterView;
        }

        public TypetyListpeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int postion);

        void onItemLongClick(View v, int postion);
    }

    /**
     * 自定义条目点击监听
     */
    private OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }

    private TextSortOnClickListener textSortOnClickListener;

    public interface TextSortOnClickListener {
        void onSortClick(View v, int postion);

        void onSortClick(View v, int postion, boolean isFirst);
    }

    public void setOnSortClick(TextSortOnClickListener mOnItemClickLitener) {
        this.textSortOnClickListener = mOnItemClickLitener;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {

        if (holder instanceof TypetypeHolder) {
            ((TypetypeHolder) holder).filterView.setOnFilterClickListener(new FilterView.OnFilterClickListener() {
                @Override
                public void onFilterClick(int position) {
                    textSortOnClickListener.onSortClick(((TypetypeHolder) holder).filterView, position);
                }
            });
        } else if (holder instanceof TypetyListpeHolder) {
            ((TypetyListpeHolder) holder).real_filterView.setOnDiscountClickListener(new DiscountsView.
                    OnDiscountClickListener() {
                @Override
                public void onDiscountClick(int position) {
                    textSortOnClickListener.onSortClick(((TypetyListpeHolder) holder).real_filterView, position, false);
                }
            });
        } else if (holder instanceof TypeItemHolder) {
            if (entity != null && position >= count) {
                HomeShopEntity.DataBean.ItemsBean itemsBean = entity.get(position - count);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickLitener.onItemClick(holder.itemView, position - count);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        mOnItemClickLitener.onItemLongClick(holder.itemView, position - count);
                        return true;
                    }
                });
                ImageManager.loadUrlImage(mContext, itemsBean.getWaimai().getLogo(), ((TypeItemHolder) holder).draw_goods);
                ((TypeItemHolder) holder).title.setText(itemsBean.getWaimai().getTitle());
                ((TypeItemHolder) holder).tv_avg_score.setText(String.valueOf(itemsBean.getWaimai().getAvg_score()));
                ((TypeItemHolder) holder).tv_orders.setText("月售  " + String.valueOf(itemsBean.getWaimai().getOrders()));
                ((TypeItemHolder) holder).tv_juli_label.setText("30分钟 | " + String.valueOf(itemsBean.getWaimai().getJuli_label()));
                ((TypeItemHolder) holder).min_amount.setText
                        ("起送  ￥" + String.valueOf(itemsBean.getWaimai().getMin_amount()) + "  |  " +
                                "配送  ￥" + String.valueOf(itemsBean.getWaimai().getFreight()) + "  |  " +
                                "人均  ￥" + String.valueOf(itemsBean.getWaimai().getFreight()));
                ((TypeItemHolder) holder).rb.setRating(Float.parseFloat(itemsBean.getWaimai().getAvg_score()));
                List<HomeShopEntity.DataBean.ItemsBean.WaimaiBean.HuodongBean>
                        products = itemsBean.getWaimai().getHuodong();
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT
                        , LinearLayout.LayoutParams.WRAP_CONTENT);
                //((TypeItemHolder) holder).linearLayout.setLayoutParams(params);
                ((TypeItemHolder) holder).linearLayout.removeAllViews();
                for (int i = 0; i < products.size(); i++) {
                    LinearLayout linearLayout = (LinearLayout) View.inflate(mContext, R.layout.item_home_order_text, null);
                    TextView textView = linearLayout.findViewById(R.id.home_tag);
                    textView.setText(products.get(i).getTitle());
                    ((TypeItemHolder) holder).linearLayout.addView(linearLayout);
                }
            }
        } else if (holder instanceof TextHolder) {
            ((TextHolder) holder).ev_search.clearFocus();
            ((TextHolder) holder).ev_search.setSelected(false);

        }
    }

}
